package com.imooc.service;

import com.imooc.mapper.UsersLikeVideosMapper;
import com.imooc.mapper.UsersMapper;
import com.imooc.mapper.VideosMapper;
import com.imooc.pojo.Users;
import com.imooc.pojo.UsersLikeVideos;
import com.imooc.pojo.Videos;
import com.imooc.pojo.vo.UsersLikeVideosVo;
import com.imooc.pojo.vo.VideosVo;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
@SuppressWarnings("all")
public class UserLikeVideoServiceImpl implements UserLikeVideoService {


    @Autowired
    private UsersLikeVideosMapper usersLikeVideosMapper;

    @Autowired
    private VideosMapper videosMapper;
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private Sid sid;

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public UsersLikeVideosVo like(String userId, String videoId) {

        Users users = usersMapper.selectByPrimaryKey(userId);
        Videos videos = videosMapper.selectByPrimaryKey(videoId);

        Example usersLikeVideosExample = new Example(UsersLikeVideos.class);
        Example.Criteria criteria = usersLikeVideosExample.createCriteria();
        criteria.andEqualTo("userId", userId);
        criteria.andEqualTo("videoId", videoId);

        UsersLikeVideos usersLikeVideos = usersLikeVideosMapper.selectOneByExample(usersLikeVideosExample);


        UsersLikeVideosVo usersLikeVideosVo = new UsersLikeVideosVo();
        if (users == null || videos == null) {
            usersLikeVideosVo.setStatus(-1);
            usersLikeVideosVo.setMessage("用户或视频不存在");
        } else {

            //根据查询出的视频的用户id 更新收到的喜欢
            String userId1 = videos.getUserId();
            Users users1 = usersMapper.selectByPrimaryKey(userId1);
            Long likeCounts = videos.getLikeCounts();

            Integer receiveLikeCounts = users1.getReceiveLikeCounts();

            //判断查询出来的是不是空的
            if (usersLikeVideos == null) {
                //之前没有点赞 现在需要点赞
                UsersLikeVideos usersLikeVideos1 = new UsersLikeVideos();
                usersLikeVideos1.setId(sid.nextShort().toString());
                usersLikeVideos1.setUserId(userId);
                usersLikeVideos1.setVideoId(videoId);
                usersLikeVideosMapper.insert(usersLikeVideos1);
                // 视频的点赞次数加1
                //判断是否为空

                likeCounts = likeCounts + 1;


                receiveLikeCounts = receiveLikeCounts + 1;


                usersLikeVideosVo.setStatus(1);
                usersLikeVideosVo.setMessage("点赞成功");
            } else {
                //取消点赞

                usersLikeVideosMapper.deleteByPrimaryKey(usersLikeVideos.getId());
                // 视频的点赞次数-1
                likeCounts = likeCounts - 1;
                receiveLikeCounts = receiveLikeCounts - 1;
                usersLikeVideosVo.setStatus(0);
                usersLikeVideosVo.setMessage("取消点赞");
            }

            videos.setLikeCounts(likeCounts);
            videosMapper.updateByPrimaryKey(videos);
            users1.setReceiveLikeCounts(receiveLikeCounts);
            usersMapper.updateByPrimaryKey(users1);
        }


        return usersLikeVideosVo;
    }

    @Override
    public List<VideosVo> likesList(String userId) {
        return null;
    }
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Boolean isLikeVideo(String userId, String videoId) {
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(videoId)) {
            return false;
        }
        Example usersLikeVideosExample = new Example(UsersLikeVideos.class);
        Example.Criteria criteria = usersLikeVideosExample.createCriteria();
        criteria.andEqualTo("userId", userId);
        criteria.andEqualTo("videoId", videoId);
        UsersLikeVideos usersLikeVideos = usersLikeVideosMapper.selectOneByExample(usersLikeVideosExample);

        return usersLikeVideos != null;
    }
}
